package com.boil.qz.safekavass.repository;

import com.boil.qz.safekavass.model.ResourceEntity;
import org.springframework.data.jpa.repository.JpaRepository;
import org.springframework.data.jpa.repository.Query;

import java.util.List;

/**
 * Created by ChenYong on 2017-07-11.
 * <br>
 * 资源实体仓库。
 */
public interface ResourceRepository extends JpaRepository<ResourceEntity, Long> {
    /**
     * 根据资源编号 <code>List</code> 查询资源实例 <code>List</code>。
     *
     * @param ids 资源编号 <code>List</code>
     * @return 资源实例 <code>List</code>
     */
    @Query(value = "select resource from ResourceEntity resource where resource.deletedCode = 0 and resource.id in ?1")
    public List<ResourceEntity> findByIds(List<Long> ids);

    /**
     * 根据权限代码统计资源个数。
     *
     * @param permissionCode 权限代码
     * @return 资源个数
     */
    @Query(value = "select count(resource) from ResourceEntity resource where resource.deletedCode = 0 and resource.permissionCode = ?1")
    public long countByPermissionCode(String permissionCode);

    /**
     * 根据父编号和资源类型查询资源。
     *
     * @param parentId     父编号
     * @param resourceType 资源类型
     * @return 资源实例 <code>List</code>
     */
    @Query(value = "select resource from ResourceEntity resource where resource.parent.id = ?1 and resource.resourceType = ?2")
    public List<ResourceEntity> findByParentIdAndResourceType(long parentId, int resourceType);
}